using Godot;

[Tool]
public partial class ChromaBox : RigidBody2D, IGravityTarget
{
    [ExportGroup("Settings")]
    [Export]
    public Vector2 Size
    {
        get => _size;
        set { _size = value; UpdateBox(); }
    }
    private Vector2 _size = new Vector2(64, 64);

    [Export]
    public BlockColor ColorType
    {
        get => _colorType;
        set { _colorType = value; UpdateBox(); }
    }
    private BlockColor _colorType = BlockColor.Red;

    [ExportGroup("Internal Refs")]
    [Export] public Sprite2D VisualSprite;
    [Export] public CollisionShape2D Collider;

    private const int LAYER_WORLD = 1;
    private const int LAYER_RED = 2;
    private const int LAYER_BLUE = 3;

    private const int LAYER_GREEN = 4;

    public override void _Ready()
    {
        UpdateBox();
    }

    private void UpdateBox()
    {
        // 1. 更新大小
        if (VisualSprite != null)
            VisualSprite.RegionRect = new Rect2(Vector2.Zero, _size);

        if (Collider != null && Collider.Shape is RectangleShape2D rect)
            rect.Size = _size;

        // 2. 更新物理 (Layer & Mask)
        Color visualColor = Colors.White;
        uint newMask = 0;

        // 重置 Layer
        SetCollisionLayerValue(LAYER_WORLD, false);
        SetCollisionLayerValue(LAYER_RED, false);
        SetCollisionLayerValue(LAYER_BLUE, false);
        SetCollisionLayerValue(LAYER_GREEN, false);

        // 基础：箱子必须能撞到地板 (World)，否则会穿过世界掉下去
        newMask |= (1 << (LAYER_WORLD - 1));

        switch (_colorType)
        {
            case BlockColor.Red:
                visualColor = Colors.Red;
                // Layer: 我是红色物体
                SetCollisionLayerValue(LAYER_RED, true);
                // Mask: 我撞地板 + 我撞红色物体 (同色互斥)
                newMask |= (1 << (LAYER_RED - 1));
                break;

            case BlockColor.Blue:
                visualColor = Colors.Cyan;
                // Layer: 我是蓝色物体
                SetCollisionLayerValue(LAYER_BLUE, true);
                // Mask: 我撞地板 + 我撞蓝色物体
                newMask |= (1 << (LAYER_BLUE - 1));
                break;

            case BlockColor.Green:
                visualColor = Colors.Lime;
                // Layer: 我是绿色物体
                SetCollisionLayerValue(LAYER_GREEN, true);
                // Mask: 我撞地板 + 我撞绿色物体
                newMask |= (1 << (LAYER_GREEN - 1));
                break;

            case BlockColor.World:
                visualColor = Colors.White;
                SetCollisionLayerValue(LAYER_WORLD, true);
                // 普通箱子撞所有东西
                newMask |= (1 << (LAYER_RED - 1));
                newMask |= (1 << (LAYER_BLUE - 1));
                newMask |= (1 << (LAYER_GREEN - 1));
                break;
        }

        // 应用 Mask
        CollisionMask = newMask;

        // 更新视觉
        if (VisualSprite != null) VisualSprite.Modulate = visualColor;

        UpdateSound();
    }



    [ExportGroup("Audio")]
    [Export] public AudioStreamPlayer2D HumSound;
    private void UpdateSound()
    {

        if (HumSound == null) return;
        if (Engine.IsEditorHint() || !IsInsideTree())
        {
            return;
        }

        float targetPitch = 1.0f;

        switch (ColorType)
        {
            case BlockColor.Red:
                targetPitch = Cfg.RedPitch;
                break;
            case BlockColor.Blue:
                targetPitch = Cfg.BluePitch;
                break;
            case BlockColor.Green:
                targetPitch = Cfg.GreenPitch;
                break;
            case BlockColor.World:
                targetPitch = 1.0f;
                break;
        }
        HumSound.PitchScale = targetPitch;
        // 2. 确保它在播放
        if (!HumSound.Playing && !Engine.IsEditorHint())
        {
            HumSound.Play();
        }
    }


    #region IGravityTarget Implementation

    public Vector2 GetVelocity() => LinearVelocity;

    public void AddExternalVelocity(Vector2 force, double delta)
    {
        // 箱子的受力逻辑
        LinearVelocity += force * (float)delta;


    }

    #endregion
}